"use strict";

goog.require("Blockly.Blocks");
goog.require("Blockly");
goog.require("Blockly.JavaScript");

Blockly.Blocks["music_text"] = {
  init: function() {
    this.jsonInit({
      message0: "选择音符 %1",
      args0: [
        {
          type: "field_dropdown",
          name: "INPUT",
          options: [
            ["C3", "C3"],
            ["D3", "D3"],
            ["F3", "F3"],
            ["G3", "G3"],
            ["A3", "A3"],
            ["B3", "B3"],
            ["C4", "C4"],
            ["D4", "D4"]
          ]
        }
      ],
      previousStatement: null,
      nextStatement: null,
      style: "text_blocks",
      tooltip: Blockly.Msg["TEXT_PRINT_TOOLTIP"],
      helpUrl: Blockly.Msg["TEXT_PRINT_HELPURL"]
    });
  }
};

Blockly.JavaScript["music_text"] = function(block) {
  // Print statement.
  // var msg =
  //   Blockly.JavaScript.valueToCode(
  //     block,
  //     "TEXT",
  //     Blockly.JavaScript.ORDER_NONE
  //   ) || "''";
  var msg = block.getFieldValue("INPUT") || "''";
  var code = JSON.stringify(msg);
  //注意此处传参市字符串""code"
  return "musicAudio(" + code + ");\n";
};

function musicAudio(step) {
  //拼接路径
  var src = "../music/" + step.toLowerCase() + ".mp3";
  //创建媒体对象
  var audio = new Audio(src);
  //调用play方法
  audio.play();
  return true;
}
